	<?php
	ob_end_clean();
	ob_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="Content-Script-Type" content="text/javascript" />
		<meta http-equiv="Content-Style-Type" content="text/css" />

	</head>
<body>
<?php
	require_once( '../system/smarty.inc' );
	require_once( '../system/mdao/Class_M_COST_DM.php' );
	require_once( '../system/mdao/Class_M_OTHER.php' );
	require_once( '../system/Class_FILE_CSV.php' );
	require_once( '../system/Class_ERROR.php' );
	require_once( '../system/login.inc.php' );
	require_once( '../system/aoki_m.php' );
	require_once( '../system/Class_PWD.php' );
	
	Class_PWD::valid_auth(AOKI_AUTH_COST_DM,$login['staff_auth']);

	$docsv_import = FALSE;
	$forms = $_POST;
	$csvf = Class_FILE_CSV::get_upload_file();
	
	$martrix_cd   =  $_POST['week_cd'];
				
	$begin_date  =   $_POST['begin_date'];
	$end_date    =   $_POST['end_date'];
	
	
	$err = new Class_ERROR("","");
	
	if ($errmsg = $err->ymd_check($begin_date)){
		Class_FILE_CSV::write_msg_error( "セール開始日 : ".$errmsg );
		exit;
	}
	if ($errmsg = $err->ymd_check($end_date)){
		Class_FILE_CSV::write_msg_error( "セール終了日 : ".$errmsg );
		exit;
	}
	$begin_date =  Class_PWD::date_to_int8 ( $begin_date );
	$end_date   =  Class_PWD::date_to_int8 ( $end_date );
	
	$mst = Class_M_OTHER::get_by_matrix_cd( $martrix_cd );

	$dm_date = $mst["flier_date"];
	
	$hvalues = array();
	$hvalues["start_time"] = $rows['ndate'];
	$end_time = date("Y-m-d H:i:s",time());
	if ($csvf) {
		// ロケール情報の変更
		$locale = setlocale(LC_ALL, 0);
		setlocale(LC_ALL,'ja_JP.UTF-8');
		
		$rowall = Class_FILE_CSV::fget_rows($csvf);
		
		if  (!empty($rowall)) {
			$col_heads = array(
				'dm_no'         , //差替ＮＯ
				'title'         , //タイトル
				'sort'          , //種類名
				'haga_out'      , //ハガキ表
				'haga_in'       , //ハガキ裏
				'futou'         , //封筒
				'preken_out'    , //プレ券表
				'preken_in'     , //プレ券裏
				'wariken_out'   , //割引券表
				'wariken_in'    , //割引券裏
				'atemei_out'    , //宛名表
				'atemei_in'     , //宛名裏
				'pera1_out'     , //ペラ１表
				'pera1_in'      , //ペラ１裏
				'pera2_out'     , //ペラ2表
				'pera2_in'      , //ペラ2裏
				'ayisatu_out'   , //挨拶状 表
				'ayisatu_in'    , //挨拶状 裏
				'honjyou_out'   , //本状表
				'honjyou_in'    , //本状裏
				'other1'        , //その他１
				'other2'        , //その他２
				'other3'        , //その他３
				'other4'        , //その他４
				'other5'        , //その他５
			);

				
			$csvheads    = array();
			$col_index   = array();
			foreach ($col_heads as $k => $v) {
				if (!empty($v)) {
					$csvheads[$k]  = $v;
					$col_index[$v] = $k;
				}
			} 
			
			$key1 = $col_index['dm_no'];//キーです、必ず仕様します
			
			$index_sort = $col_index['sort'];

			$dao = new Class_M_COST_DM;
			
			$ngline      = 0;
			$error_times = 0;
			$error_stop  = IMPORT_STOP_MAX_ERROR_TIMES;
			$errtext     = array();
			$isng        = FALSE;
			$chk         = $dao->get_checks();
			$sort_dm_values = array(); 
			foreach ( $aoki_dm_sort as $kk => $vv) {
				$sort_dm_values[$vv] = $kk;
			}
			//print_r($sort_dm_values);
			
			foreach ($rowall as $line => $row) {
				$isng = FALSE;
				//CSV内部重複不可チェック
				foreach ($rowall as $kk=>$vv) {
					if  ($line != $kk) {
						if ($row[$key1] == $vv[$key1]){
							$col = $key1 + 1;
							$isng = TRUE;
							$ngMsg_tmp[$line][] = "{$line}行と{$kk}行の{$col}列目の値重複不可。";
							$error_times++;
							if ($error_times > $error_stop) {
								break;
							}
						}
					}
				} 
				
				//一般チェック
				foreach ( $csvheads as $k=>$v ) {
					$msg =  $err->check( $rowall[$line][$k], $chk[$v ] ); 
					if (!empty($msg)) {
						$isng = TRUE;
						$col  = $k+1;
						$ngMsg_tmp[$line][] = "{$line}行{$col}列目：{$msg}。";
						$error_times++;					
						if ($error_times > $error_stop) {
							break;
						}
					}
				}
					//種類
				if ( !empty($sort_dm_values[ $row[$index_sort]] ) ) {
					$rowall[$line][$index_sort] = $sort_dm_values[ $row[$index_sort] ];
				} else {
					$isng = TRUE;
					$col = $index_sort + 1;
					$ngMsg_tmp[$line][] = "{$line}行{$col}列目[{$row[$index_sort]}]は存在しない。";
					$error_times++;
					if ($error_times > $error_stop) {
						break;
					}
				} 
				
					
				if ( $isng ) {
					++$ngline;
					$errtext[$ngline] = $row;
					if ($error_times > $error_stop) {
						break;
					}
				}
			}
			
			if ($ngline > 0) {
				$errHtml = Class_FILE_CSV::format_msg($ngMsg_tmp);
				if ($error_times > $error_stop) {
					Class_FILE_CSV::write_msg_error( $errHtml ,$error_stop );	
				
				} else {
					Class_FILE_CSV::write_msg_error( $errHtml );	
				}
			} else {
				//DBへ更新
				
				try {
					$daodb = $dao->get_db();
					$dao->begin_trans();
					$i = 0;
					$percent = "";
					$percentold = "";
					
					foreach ( $rowall as $k=>$v ) {
						$i++;
						$percent = intval($i/$line * 100)."%";
						if  ( $percent != $percentold ) {
							Class_FILE_CSV::write_msg_doing( $percent ) ;
						}
						$percentold = $percent;
						$values  = array();
						
						foreach ( $csvheads as $kk => $vv ) {
							$values[$vv] = $v[$kk];
						}
						$values["matrix_cd"] = $martrix_cd;
						$keyvalues =  $values[$col_heads[$key1]];
						
						$where = sprintf(" {$col_heads[$key1]} = '%s' and matrix_cd ='{$martrix_cd}' ", $dao->quote($keyvalues) );
						$values["begin_date"]    = $begin_date;
						$values["end_date"]      = $end_date ;
						$values["dm_date"]       = $dm_date ;
						
						if ( $dao->exits( $where ) ) {
							if (!($dao->editbywhere($where ,$values ))) {
								throw  new Exception("mcost_dm_d_import db更新");
							}
						} else {
							if ( !( $dao->add($values ) ) ) {
								throw  new Exception("mcost_dm_d_import登録");
							}
						}
					}
					Class_FILE_CSV::write_msg_succed($line, "mcost_dm_d_import");
					$dao->commit_trans();
					
				} catch (Exception $e) {
					$dao->rollback_trans();
					Class_FILE_CSV::write_msg_error( "DB Exceptionがありました。</br>" );
				}
				
			} 
		}else {

		$errHtml = "CSVファイルエラー";
		Class_FILE_CSV::write_msg_error( $errHtml );
			
		}
		
		// ロケール情報の復元
		setlocale(LC_ALL, $locale);
	
	}else {

		$errHtml = "CSVファイルエラー";
		Class_FILE_CSV::write_msg_error( $errHtml );
	}
?>
</body>
	</himl>
	